﻿using System;
using System.Collections.Generic;
using System.Text;

namespace CMSLibrary.TagsCreate.CustomFunction.Common
{
    /// <summary>
    /// 格式化日期
    /// 1.  DateTime()  当前日期，默认格式化
    /// 2.  DateTime(格式化字符串)  当前日期，指定格式化
    /// 3.  DateTime(源日期)    指定日期，默认格式化
    /// 4.  DateTime(源日期, 格式化字符串)  指定日期，指定格式化
    /// 5.  DateTime(追加日期格式, 追加数值) 1.{year,month,day,hour,mintue,second} 2.正负整数
    /// </summary>
    public class Custom_DateTime : PFunctionBase
    {
        #region 参数成员
        /// <summary>
        /// 源日期
        /// </summary>        
        DateTime source = DateTime.Now;
        /// <summary>
        /// 格式化字符串
        /// </summary>        
        string format = "yyyy-MM-dd";
        /// <summary>
        /// 追加日期格式
        /// </summary>
        string datetype = "day";
        /// <summary>
        /// 追加数值
        /// </summary>
        int AppendValue = 1;
        #endregion

        #region 重写的方法
        protected override void SplitFormatParas(string sourceParas)
        {
            string[] paras = GetParasArray(sourceParas);
            //确定重载方法,由多到少 
            /********** 5 **********/
            try
            {
                datetype = paras[0];
                AppendValue = Convert.ToInt32(paras[1]);
                FunctionBurden = BurdenIndex.five;
                return;
            }
            catch { }
            /********** 4 **********/
            try
            {
                source = Convert.ToDateTime(paras[0]);
                format = Convert.ToString(paras[1]);
                FunctionBurden = BurdenIndex.four;
                return;
            }
            catch { }
            /********** 3 **********/
            try
            {
                source = Convert.ToDateTime(paras[0]);                
                FunctionBurden = BurdenIndex.three;
                return;
            }
            catch { }
            /********** 2 **********/
            try
            {                
                format = Convert.ToString(paras[0]);
                FunctionBurden = BurdenIndex.two;
                return;
            }
            catch { }
            /********** 1 **********/
            try
            {                
                FunctionBurden = BurdenIndex.one;
                return;
            }
            catch { }
        }

        public override string ToString()
        {            
            if (FunctionBurden == BurdenIndex.five)
            {
                switch (datetype)
                { 
                    case "y":
                        source = source.AddYears(AppendValue);
                        break;
                    case "M":
                        source = source.AddMonths(AppendValue);
                        break;
                    case "d":
                        source = source.AddDays(Convert.ToDouble(AppendValue));
                        break;
                    case "h":
                        source = source.AddHours(Convert.ToDouble(AppendValue));
                        break;
                    case "m":
                        source = source.AddMinutes(Convert.ToDouble(AppendValue));
                        break;
                    case "s":
                        source = source.AddSeconds(Convert.ToDouble(AppendValue));
                        break;
                }
            }
            
            return source.ToString(format);            
        }
        #endregion

    }
}
